package cn.wolfcode.mq.linstener;

import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMQResult;
import cn.wolfcode.redis.CommonRedisKey;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author: 普发忠
 * @Date: 2021/11/11/19:38
 */
@Component
@RocketMQMessageListener(
        topic = MQConstant.CANCEL_SECKILL_PERSION,
        consumerGroup = "cancel_seckill_persion-consumer",
        messageModel= MessageModel.BROADCASTING
)
public class CancelSeckillPersion implements RocketMQListener<OrderMQResult> {
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @Override
    public void onMessage(OrderMQResult ret) {
        String strObj = redisTemplate.opsForValue().get(CommonRedisKey.USER_TOKEN.getRealKey(ret.getToken()));
        UserInfo userInfo = JSON.parseObject(strObj, UserInfo.class);
        //删除秒杀者
        String orderSetRealKey = SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(String.valueOf(ret.getSeckillId()));
        redisTemplate.opsForSet().remove(orderSetRealKey,String.valueOf(userInfo.getPhone()));
    }
}
